package com.cagtwitter.hibernate;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.exception.SQLGrammarException;

import com.cagtwitter.services.BaseParams;

public class CelebrityDao {
	public void addCelebrityUser(Celebrity celebrity){
		Session session = null;
		try{
			session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = null;
			
			transaction = session.beginTransaction();
			session.save(celebrity);
			transaction.commit();
		}
		catch(SQLGrammarException sq){
			sq.getSQLException().getNextException().printStackTrace();
		}
		finally{
			session.close();
		}
	}

	public List<Celebrity> getCelebrities(){
		Session session=null;
		List<Celebrity> celebList = null;
		try{
			session = HibernateUtil.getSessionFactory().openSession();
			celebList = session.createQuery("from Celebrity").list();
		}
		catch (HibernateException e) {
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return celebList;
	}
	
	public List<Celebrity> getCelebritiesMatches(String match_string){
		Session session=null;
		List<Celebrity> celebList = null;
		try{
			session = HibernateUtil.getSessionFactory().openSession();
			celebList = session.createQuery("from Celebrity where firstname like'"+match_string+"%'").list();
		}
		catch (HibernateException e) {
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return celebList;
	}
	
	// Celebrity Tweet Table
	
	public void addCelebrityTweet(CelebTweet celebTweet){
		Session session = null;
		try{
			session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = null;
			
			transaction = session.beginTransaction();
			session.save(celebTweet);
			transaction.commit();
		}
		catch(SQLGrammarException sq){
			sq.getSQLException().getNextException().printStackTrace();
		}
		finally{
			session.close();
		}
	}
	
	public List<CelebTweet> getCelebrityTweets(String name,int page,int count){
		Session session=null;
		List<CelebTweet> celebTweetList = null;
		try{
			session = HibernateUtil.getSessionFactory().openSession();
			Query query = session.createQuery("from CelebTweet where name='"+name+"'");
			query.setFirstResult((page-1) * count);
			query.setMaxResults(count);
			celebTweetList = query.list();
			
		}
		catch (HibernateException e) {
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return celebTweetList;
	}
	
	public int getCelebrityTweetCount(String name,String type){
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
	    String selectQuery="";
	    
		if(type.equals(BaseParams.CELEBRITY)) selectQuery = "select count(*) from CelebTweet where name='" + name + "'";
		else if(type.equals(BaseParams.ABOUT)) selectQuery = "select count(*) from AboutCelebTweet where name='" + name + "'";
	    int count = 0;
	    Query query = session.createQuery(selectQuery);
	    
	    
	    count = Integer.parseInt(query.uniqueResult().toString());
	    
	    session.close();
	    return count;
	}
	
	public void setLastTwid(String name,String id,String type){
		Session session=null;
		List<Celebrity> celebList = null;
		Transaction transaction = null;
		
		try{
			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			celebList = session.createQuery("from Celebrity where username='" + name + "'").list();
			
			if(celebList.size() > 0){
				Celebrity celeb = celebList.get(0);
				if(type.equals(BaseParams.CELEBRITY)) celeb.setLast_twid(id);
				else if(type.equals(BaseParams.ABOUT)) celeb.setAboutLast_twid(id);
				
				session.update(celeb);
				transaction.commit();
			}
		}
		catch (HibernateException e) {
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		
	}
	
	public String getLastTwid(String name,String type){
		Session session=null;
		List<Celebrity> celebList = null;
		try{
			session = HibernateUtil.getSessionFactory().openSession();
			celebList = session.createQuery("from Celebrity where username='" + name + "'").list();
			
			if(celebList.size() > 0){
				Celebrity celeb = celebList.get(0);

				if(type.equals(BaseParams.CELEBRITY)) return celeb.getLast_twid();
				else if(type.equals(BaseParams.ABOUT)) return celeb.getAboutLast_twid();
			}
		}
		catch (HibernateException e) {
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return "0";
	}
	
	public String getLastTweetOfCelebrity(String name){
		Session session=null;
		List<CelebTweet> celebList = null;
		CelebTweet celeb=null;
		try{
			session = HibernateUtil.getSessionFactory().openSession();
			celebList = session.createQuery("select act from Celebrity cel,CelebTweet act where cel.last_twid = act.twid AND cel.username = '"+name+"'").list();
			
			if(celebList.size() > 0){
				celeb = celebList.get(0);
				return celeb.getTweettext();
			}
			
		}
		catch (HibernateException e) {
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return "";
	}
	
	// Celebrity About Tweet Table
	
	public void addAboutCelebrityTweet(AboutCelebTweet aboutCelebTweet){
		Session session = null;
		try{
			session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = null;
			
			transaction = session.beginTransaction();
			session.save(aboutCelebTweet);
			transaction.commit();
		}
		catch(SQLGrammarException sq){
			sq.getSQLException().getNextException().printStackTrace();
		}
		finally{
			session.close();
		}
	}
	
	public List<AboutCelebTweet> getAboutCelebrityTweets(String name,int page,int count){
		Session session=null;
		List<AboutCelebTweet> aboutCelebTweetList = null;
		try{
			session = HibernateUtil.getSessionFactory().openSession();
			Query query = session.createQuery("from AboutCelebTweet where name='"+name+"' order by twid desc");
			query.setFirstResult((page-1) * count);
			query.setMaxResults(count);
			
			aboutCelebTweetList = query.list();
			
		}
		catch (HibernateException e) {
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return aboutCelebTweetList;
	}
	
	public boolean isAnyCelebTweetWithId(String twid,String table){
		Session session=null;
		List<CelebTweet> celebList = null;
		try{
			session = HibernateUtil.getSessionFactory().openSession();
			celebList = session.createQuery("from "+table+" ct where ct.twid = '"+twid+"'").list();
			
			if(celebList.size() > 0){
				return true;
			}
			
		}
		catch (HibernateException e) {
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return false;
	}
}
